package com.cskaoyan;

import com.cskaoyan.bean.City;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class MybatisMain {

    public static void main(String[] args) throws IOException {

        // 如何去构建SQLsession呢？
        // 1. 获取SQLsessionFactoryBuilder  // 建造者模式
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        // 2. 获取SqlSessionFactory // 工厂模式，是用来生产SqlSession的
        ClassLoader classLoader = MybatisMain.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("mybatis-config.xml");

        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory sqlSessionFactory = builder.build(in);

        // 3.  生产sqlSession sqlSession可以理解为之间JDBC里面的connection，sqlSession可以用来去执行SQL语句
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 4. sqlSession去执行SQL语句
        City city = sqlSession.selectOne("cskaoyan.selectCityById", 5);

        // 5. 输出
        System.out.println(city);
    }
}
